package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.domain.vo.RefundOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.RefundOrderInfo;
import com.ruoyi.system.mapper.RefundOrderMapper;
import com.ruoyi.system.domain.RefundOrder;
import com.ruoyi.system.service.IRefundOrderService;

/**
 * 退货单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-17
 */
@Service
public class RefundOrderServiceImpl implements IRefundOrderService 
{
    @Autowired
    private RefundOrderMapper refundOrderMapper;

    /**
     * 查询退货单
     * 
     * @param id 退货单主键
     * @return 退货单
     */
    @Override
    public RefundOrder selectRefundOrderById(Long id)
    {
        return refundOrderMapper.selectRefundOrderById(id);
    }

    /**
     * 查询退货单列表
     * 
     * @param refundOrder 退货单
     * @return 退货单
     */
    @Override
    public List<RefundOrderVo> selectRefundOrderList(RefundOrder refundOrder)
    {
        return refundOrderMapper.selectRefundOrderList(refundOrder);
    }

    /**
     * 新增退货单
     * 
     * @param refundOrder 退货单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRefundOrder(RefundOrder refundOrder)
    {
        refundOrder.setCreateTime(DateUtils.getNowDate());
        int rows = refundOrderMapper.insertRefundOrder(refundOrder);
        insertRefundOrderInfo(refundOrder);
        return rows;
    }

    /**
     * 修改退货单
     * 
     * @param refundOrder 退货单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRefundOrder(RefundOrder refundOrder)
    {
        refundOrderMapper.deleteRefundOrderInfoByReturnOrderId(refundOrder.getId());
        insertRefundOrderInfo(refundOrder);
        return refundOrderMapper.updateRefundOrder(refundOrder);
    }

    /**
     * 批量删除退货单
     * 
     * @param ids 需要删除的退货单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRefundOrderByIds(Long[] ids)
    {
        refundOrderMapper.deleteRefundOrderInfoByReturnOrderIds(ids);
        return refundOrderMapper.deleteRefundOrderByIds(ids);
    }

    /**
     * 删除退货单信息
     * 
     * @param id 退货单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRefundOrderById(Long id)
    {
        refundOrderMapper.deleteRefundOrderInfoByReturnOrderId(id);
        return refundOrderMapper.deleteRefundOrderById(id);
    }

    /**
     * 新增退货明细信息
     * 
     * @param refundOrder 退货单对象
     */
    public void insertRefundOrderInfo(RefundOrder refundOrder)
    {
        List<RefundOrderInfo> refundOrderInfoList = refundOrder.getRefundOrderInfoList();
        Long id = refundOrder.getId();
        if (StringUtils.isNotNull(refundOrderInfoList))
        {
            List<RefundOrderInfo> list = new ArrayList<RefundOrderInfo>();
            for (RefundOrderInfo refundOrderInfo : refundOrderInfoList)
            {
                refundOrderInfo.setReturnOrderId(id);
                list.add(refundOrderInfo);
            }
            if (list.size() > 0)
            {
                refundOrderMapper.batchRefundOrderInfo(list);
            }
        }
    }
}
